package com.yx.cloud.animator;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.yx.cloud.R;
import com.yx.cloud.utils.RomUtils;

public class AreaSetHelpDialog extends Dialog {
    private Context context;
    private AreaSetHelpDialog.ConfirmCallback confirmCallback;
    private int viewId = -1;
    private AreaClickView areaClickView;

    public AreaSetHelpDialog(Context context, int viewId, AreaSetHelpDialog.ConfirmCallback confirmCallback) {
        super(context, R.style.dialog);
        this.context = context;
        this.confirmCallback = confirmCallback;
        this.viewId = viewId;
    }

    public static AreaSetHelpDialog.Builder newBuilder() {
        return new AreaSetHelpDialog.Builder();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(viewId);

        LinearLayout btn_confirm = findViewById(R.id.ll_I_know_area);
        areaClickView = findViewById(R.id.click_view);
        areaClickView.ivAdd = findViewById(R.id.iv_area_add_help);
        areaClickView.cbTimes = findViewById(R.id.cb_area_clean_times_help);
        areaClickView.cbStartClean = findViewById(R.id.cb_area_start_clean_help);

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                if (confirmCallback != null) {
                    //如果confirm返回false, 不取消对话框
                    if (confirmCallback.onConfirm()) {
                        dismiss();
                    }
                } else {
                    dismiss();
                }
                areaClickView.endAnimator = true;
            }
        });

        setCanceledOnTouchOutside(false);

        areaClickView.viewType = RestrictAreaClickView.VIEW_TYPE_INXNI;
    }

    public interface ConfirmCallback {
        boolean onConfirm();

        boolean onCancel();
    }

    public static final class Builder {
        private Context context;
        private String message;
        private AreaSetHelpDialog.ConfirmCallback confirmCallback;
        private int viewId = -1;

        private Builder() {
        }

        public AreaSetHelpDialog.Builder context(Context context) {
            this.context = context;
            return this;
        }

        public AreaSetHelpDialog.Builder viewId(int viewId) {
            this.viewId = viewId;
            return this;
        }

        public AreaSetHelpDialog.Builder confirmCallback(AreaSetHelpDialog.ConfirmCallback confirmCallback) {
            this.confirmCallback = confirmCallback;
            return this;
        }

        public AreaSetHelpDialog build() {
            if (context == null) {
                throw new IllegalArgumentException("context should not be null");
            }
            AreaSetHelpDialog areaSetHelpDialog = new AreaSetHelpDialog(context, viewId, confirmCallback);
            return areaSetHelpDialog;
        }
    }
}
